package shared.message

import org.w3c.dom._

case class FormattedMessage(val sender: String, val message: String, val color: String, val italic: Boolean, val bold: Boolean) {

  def this(s: String, m: String) = this(s, m, "auto", false, false)
  
  def italic_=(v: Boolean) = new FormattedMessage(sender, message, color, v, bold)
  
  def bold_=(v: Boolean) = new FormattedMessage(sender, message, color, italic, v)
  
  def color_=(c: String) = new FormattedMessage(sender, message, c, italic, bold)
  
  def createNode(doc: Document, receiver: String) =  {
    var node: Node = doc.createTextNode( if("" == sender) "* " + message else "[" + sender + "] " + message)
    if(italic) {
      val r = doc.createElement("i")
      r.appendChild(node)
      node = r
    }
    if(bold || message.contains(receiver) || "" == sender) {
      val r = doc.createElement("b")
      r.appendChild(node)
      node = r
    }
    if(color != null && color != "" && color != "auto") {
      val r = doc.createElement("font")
      val att = doc.createAttribute("color")
      att.setNodeValue(color)
      r.appendChild(att)
      r.appendChild(node)
      node = r
    }
    node
  }
  
}
